package org.example.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.example.common.ResponseMessage;
import org.example.entity.WorkPlan;
import org.example.entity.dto.WorkPlanDto;
import org.example.entity.dto.WorkSheetDto;
import org.example.mapper.WorkPlanMapper;
import org.example.service.WorkPlanService;
import org.example.service.WorkSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/WorkPlan")
@Tag(name = "work-sheet-controller", description = "work-sheet-controller")
public class WorkPlanController {
    @Autowired
    private WorkPlanService workPlanService;

    @Autowired
    private WorkPlanMapper workPlanMapper;

    @Autowired
    private WorkSheetService workSheetService;

    /***
     * 新增周计划
     * @param workPlanDto
     * @return
     */
    @Operation(summary = "新增周计划")
    @PostMapping("/addPlan")
    public ResponseMessage add(@RequestBody WorkPlanDto workPlanDto) {
        try{
            workPlanService.addPlan(workPlanDto);
        }catch (Exception e){
            return  ResponseMessage.error();
        }
        return ResponseMessage.success(null);

    }

    /***
     * 根据id删除周计划
     * @param ID
     * @return
     */
    @Operation(summary = "根据id删除周计划")
    @PostMapping("/delPlan/{ID}")
    public ResponseMessage del(@PathVariable Integer ID){
       return workPlanService.delByID(ID);
    }

    /***
     * 更新周计划
     * @param workPlanDto
     * @return
     */
    @Operation(summary = "更新周计划")
    @PostMapping("/updateWork")
    public ResponseMessage update(@RequestBody WorkPlanDto workPlanDto){
        workPlanService.update(workPlanDto);
        return ResponseMessage.success(null);
    }

    /***
     * 新增周表
     * @param workSheetDto
     * @return
     */
    @Operation(summary = "新增周表")
    @PostMapping("/addSheet")
    public ResponseMessage add(@RequestBody WorkSheetDto workSheetDto){
        return workSheetService.addSheet(workSheetDto);
    }

    @Operation(summary = "根据id删除周表")
    @PostMapping("/delSheet/{ID}")
    public ResponseMessage delSheet(@PathVariable Integer ID){
        return workSheetService.delByID(ID);
    }

    @Operation(summary = "导出Excel表")
    @GetMapping("/exportPlan")
    public void exportPlan(@RequestParam(required = false) String UserName, HttpServletResponse response) throws IOException {
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //第一种全部导出
        List<WorkPlan> plan = new ArrayList<>();
        if (StrUtil.isBlank(UserName)){
            plan = workPlanMapper.selectAll();
        }
        writer.write(plan,true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("周计划表","UTF-8") + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        writer.flush(outputStream,true); //数组是一个字节数组，也是文件的字节流数组
        outputStream.flush();
        outputStream.close();
    }
}
